<?php
namespace App\Crawlers;
use QL\QueryList;
use QL\Ext\Chrome;
use App\Models\MovieBaseInfo;
class MovieListCrawler {
    
    protected $url = 'https://movie.douban.com/j/new_search_subjects?sort=T&range=1,10&tags=电影&start=';
    
    public function chrome()
    {
        
    }

    public function get($page = 1) {
        $ql = QueryList::getInstance();
        $ql->use(Chrome::class, 'chrome');
        $start = ($page - 1) * 20;
        $url = $this->url . $start;
        $data = $ql->chrome(function ($page, $browser) use ($url) {
            $page->setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36');
            $page->goto($url);
            $html = $page->content();
            // 关闭浏览器
            $browser->close();
            return $html;
        })->find('pre')->text();
        $data = json_decode($data, true);
        foreach($data['data'] as $d) {
            $this->insertMovieListInfo($d);
        }
    }

    protected function insertMovieListInfo($data = [])
    {
        $insertData = [
            'image' => $data['cover'],
            'name' => $data['title'],
        ];
        $movie = new MovieBaseInfo;
        $movie->firstOrCreate(['douban_id' => $data['id']], $insertData);
    }
}